####################################
# Descriptive statistics gender
####################################

rm(list=ls())

library(Hmisc)
library(stargazer)
library(foreign)
library(ggplot2)
library(rdrobust)

################
# Prepare data 
################

# read data
load("~/Dropbox/Gender Chile/08_replication/gender_chile_2020aug28.RData")  
names(d)

# check incumbents
table(d$county,d$electionyear)

###########################
# Eligible sample
###########################
  
local = data.frame(d$permanent_temporary,
                   d$female_permanent_temporary,
                   d$male_permanent_temporary,
                   d$sharefemale_permanent_temporary)

colnames(local) = c("Municipal employees",
                    "Female municipal employees",
                    "Male municipal employees",
                    "Share of municipal employees")
colnames(local)

stargazer(local,
          type = "text",
          colnames = FALSE,
          min.max = FALSE,
          title="", 
          summary.stat = c("mean", "sd","n"),
          digits=2, 
          rownames=FALSE, 
          align=TRUE, 
          float = TRUE, 
          float.env = "table", 
          table.placement = "H",
          header = FALSE, 
          no.space = TRUE)

###############################################################
# Subsetting
################################################################

h1 = rdbwselect(d$permanent_temporary,d$margin,cluster = d$cluster)$bws[1]
h2 = rdbwselect(d$female_permanent_temporary,d$margin,cluster = d$cluster)$bws[1]
h3 = rdbwselect(d$male_permanent_temporary,d$margin,cluster = d$cluster)$bws[1]
h4 = rdbwselect(d$sharefemale_permanent_temporary,d$margin,cluster = d$cluster)$bws[1]

d1 = d[abs(d$margin)<=h1,]
d2 = d[abs(d$margin)<=h2,]
d3 = d[abs(d$margin)<=h3,]
d4 = d[abs(d$margin)<=h4,]

# check country
describe(d_original$county)
describe(d4$county)

###########################
# Gender comparison
###########################

permanent_temporary_d_original = mean(d_original$permanent_temporary, na.rm = T)
female_permanent_temporary_d_original = mean(d_original$female_permanent_temporary, na.rm = T)
male_permanent_temporary_d_original = mean(d_original$male_permanent_temporary, na.rm = T)
sharefemale_permanent_temporary_d_original = mean(d_original$sharefemale_permanent_temporary, na.rm = T)
pinera06_2_p_d_original = mean(d_original$pinera06_2_p, na.rm = T)
bachelet06_2_p_d_original = mean(d_original$bachelet06_2_p, na.rm = T)
idh98_d_original = mean(d_original$idh98, na.rm = T)
rankingsalud98_d_original = mean(d_original$rankingsalud98, na.rm = T)
rankingeducacion98_d_original = mean(d_original$rankingeducacion98, na.rm = T)
rankingingreso98_d_original = mean(d_original$rankingingreso98, na.rm = T)
countysize_d_original = mean(d_original$countysize, na.rm = T)
distance_d_original = mean(d_original$distance, na.rm = T)

permanent_temporary_d = mean(d$permanent_temporary, na.rm = T)
female_permanent_temporary_d = mean(d$female_permanent_temporary, na.rm = T)
male_permanent_temporary_d = mean(d$male_permanent_temporary, na.rm = T)
sharefemale_permanent_temporary_d = mean(d$sharefemale_permanent_temporary, na.rm = T)
pinera06_2_p_d = mean(d$pinera06_2_p, na.rm = T)
bachelet06_2_p_d = mean(d$bachelet06_2_p, na.rm = T)
idh98_d = mean(d$idh98, na.rm = T)
rankingsalud98_d = mean(d$rankingsalud98, na.rm = T)
rankingeducacion98_d = mean(d$rankingeducacion98, na.rm = T)
rankingingreso98_d = mean(d$rankingingreso98, na.rm = T)
countysize_d = mean(d$countysize, na.rm = T)
distance_d = mean(d$distance, na.rm = T)

permanent_temporary_d1 = mean(d1$permanent_temporary, na.rm = T)
female_permanent_temporary_d1 = mean(d1$female_permanent_temporary, na.rm = T)
male_permanent_temporary_d1 = mean(d1$male_permanent_temporary, na.rm = T)
sharefemale_permanent_temporary_d1 = mean(d1$sharefemale_permanent_temporary, na.rm = T)
pinera06_2_p_d1 = mean(d1$pinera06_2_p, na.rm = T)
bachelet06_2_p_d1 = mean(d1$bachelet06_2_p, na.rm = T)
idh98_d1 = mean(d1$idh98, na.rm = T)
rankingsalud98_d1 = mean(d1$rankingsalud98, na.rm = T)
rankingeducacion98_d1 = mean(d1$rankingeducacion98, na.rm = T)
rankingingreso98_d1 = mean(d1$rankingingreso98, na.rm = T)
countysize_d1 = mean(d1$countysize, na.rm = T)
distance_d1 = mean(d1$distance, na.rm = T)

permanent_temporary_d2 = mean(d2$permanent_temporary, na.rm = T)
female_permanent_temporary_d2 = mean(d2$female_permanent_temporary, na.rm = T)
male_permanent_temporary_d2 = mean(d2$male_permanent_temporary, na.rm = T)
sharefemale_permanent_temporary_d2 = mean(d2$sharefemale_permanent_temporary, na.rm = T)
pinera06_2_p_d2 = mean(d2$pinera06_2_p, na.rm = T)
bachelet06_2_p_d2 = mean(d2$bachelet06_2_p, na.rm = T)
idh98_d2 = mean(d2$idh98, na.rm = T)
rankingsalud98_d2 = mean(d2$rankingsalud98, na.rm = T)
rankingeducacion98_d2 = mean(d2$rankingeducacion98, na.rm = T)
rankingingreso98_d2 = mean(d2$rankingingreso98, na.rm = T)
countysize_d2 = mean(d2$countysize, na.rm = T)
distance_d2 = mean(d2$distance, na.rm = T)

permanent_temporary_d3 = mean(d3$permanent_temporary, na.rm = T)
female_permanent_temporary_d3 = mean(d3$female_permanent_temporary, na.rm = T)
male_permanent_temporary_d3 = mean(d3$male_permanent_temporary, na.rm = T)
sharefemale_permanent_temporary_d3 = mean(d3$sharefemale_permanent_temporary, na.rm = T)
pinera06_2_p_d3 = mean(d3$pinera06_2_p, na.rm = T)
bachelet06_2_p_d3 = mean(d3$bachelet06_2_p, na.rm = T)
idh98_d3 = mean(d3$idh98, na.rm = T)
rankingsalud98_d3 = mean(d3$rankingsalud98, na.rm = T)
rankingeducacion98_d3 = mean(d3$rankingeducacion98, na.rm = T)
rankingingreso98_d3 = mean(d3$rankingingreso98, na.rm = T)
countysize_d3 = mean(d3$countysize, na.rm = T)
distance_d3 = mean(d3$distance, na.rm = T)

permanent_temporary_d4 = mean(d4$permanent_temporary, na.rm = T)
female_permanent_temporary_d4 = mean(d4$female_permanent_temporary, na.rm = T)
male_permanent_temporary_d4 = mean(d4$male_permanent_temporary, na.rm = T)
sharefemale_permanent_temporary_d4 = mean(d4$sharefemale_permanent_temporary, na.rm = T)
pinera06_2_p_d4 = mean(d4$pinera06_2_p, na.rm = T)
bachelet06_2_p_d4 = mean(d4$bachelet06_2_p, na.rm = T)
idh98_d4 = mean(d4$idh98, na.rm = T) 
rankingsalud98_d4 = mean(d4$rankingsalud98, na.rm = T)
rankingeducacion98_d4 = mean(d4$rankingeducacion98, na.rm = T)
rankingingreso98_d4 = mean(d4$rankingingreso98, na.rm = T)
countysize_d4 = mean(d4$countysize, na.rm = T)
distance_d4 = mean(d4$distance, na.rm = T)

mean_d_original = round(c(permanent_temporary_d_original,
                          female_permanent_temporary_d_original,
                          male_permanent_temporary_d_original,
                          sharefemale_permanent_temporary_d_original,
                          pinera06_2_p_d_original,
                          bachelet06_2_p_d_original,
                          idh98_d_original,
                          rankingsalud98_d_original,
                          rankingeducacion98_d_original,
                          rankingingreso98_d_original,
                          countysize_d_original,
                          distance_d_original),2)

mean_d = round(c(permanent_temporary_d, 
                 female_permanent_temporary_d, 
                 male_permanent_temporary_d, 
                 sharefemale_permanent_temporary_d,
                 pinera06_2_p_d,
                 bachelet06_2_p_d,
                 idh98_d,
                 rankingsalud98_d,
                 rankingeducacion98_d,
                 rankingingreso98_d,
                 countysize_d,
                 distance_d),2)

mean_d1 = round(c(permanent_temporary_d1, 
                  female_permanent_temporary_d1, 
                  male_permanent_temporary_d1, 
                  sharefemale_permanent_temporary_d1,
                  pinera06_2_p_d1,
                  bachelet06_2_p_d1,
                  idh98_d1,
                  rankingsalud98_d1,
                  rankingeducacion98_d1,
                  rankingingreso98_d1,
                  countysize_d1,
                  distance_d1),2)

mean_d2 = round(c(permanent_temporary_d2, 
                  female_permanent_temporary_d2, 
                  male_permanent_temporary_d2, 
                  sharefemale_permanent_temporary_d2,
                  pinera06_2_p_d2,
                  bachelet06_2_p_d2,
                  idh98_d2,
                  rankingsalud98_d2,
                  rankingeducacion98_d2,
                  rankingingreso98_d2,
                  countysize_d2,
                  distance_d2),2)

mean_d3 = round(c(permanent_temporary_d3, 
                  female_permanent_temporary_d3, 
                  male_permanent_temporary_d3, 
                  sharefemale_permanent_temporary_d3,
                  pinera06_2_p_d3,
                  bachelet06_2_p_d3,
                  idh98_d3,
                  rankingsalud98_d3,
                  rankingeducacion98_d3,
                  rankingingreso98_d3,
                  countysize_d3,
                  distance_d3),2)

mean_d4 = round(c(permanent_temporary_d4, 
                  female_permanent_temporary_d4, 
                  male_permanent_temporary_d4, 
                  sharefemale_permanent_temporary_d4,
                  pinera06_2_p_d4,
                  bachelet06_2_p_d4,
                  idh98_d4,
                  rankingsalud98_d4,
                  rankingeducacion98_d4,
                  rankingingreso98_d4,
                  countysize_d4,
                  distance_d4),2)

colnames = c("Municipal employees",
             "Female municipal employees",
             "Male municipal employees",
             "Share of female municipal employees",
             "Right-wing candidate",
             "Left-wing candidate",
             "Development",
             "Health",
             "Education",
             "Income",
             "County size",
             "Distance")

table = data.frame(colnames,mean_d_original,mean_d,mean_d1,mean_d2,mean_d3,mean_d4)
colnames(table) <- c("Outcomes","All units",
                     "Eligible units",
                     "Bandwidth 1",
                     "Bandwidth 2",
                     "Bandwidth 3",
                     "Bandwidth 4")
table

stargazer(table, 
          type = "text",
          colnames = TRUE,
          summary = FALSE,
          title="", 
          digits=2, 
          rownames=FALSE, 
          float = TRUE, 
          float.env = "table", 
          table.placement = "H")
                     